package com.ubao.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tencent.tauth.TAuthView;
import com.ubao.R;
import com.ubao.activity.MyApp.GoBack;
import com.ubao.base.RequestBaseActivity;
import com.ubao.bean.LoginInfo;
import com.ubao.bean.Status;
import com.ubao.global.GoBackable;
import com.ubao.network.JsonParser;
import com.ubao.network.ResponseInterface;
import com.ubao.network.Url;
import com.ubao.network.req.SpecialRequest;
import com.ubao.network.resp.SpecialResponse;
import com.ubao.ui.titlebar.GTitleBar;
import com.ubao.util.Util;
import com.ubao.util.log.Logger;
import com.ubao.weibo.RenRenOAuthActivity;
import com.ubao.weibo.SinaWeiboOAuthActivity;

public class LoginActivity extends RequestBaseActivity implements GoBackable{
	GTitleBar mTitleBar;
	Button btnLogin;
	EditText etName;
	EditText etPassword;
	String name;
	String password;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ui_login);
		initViews();
	}
	private void initViews() {
		etName = (EditText) findViewById(R.id.etLoginName);
		etName.setText("zzy157@126.com");
		etPassword = (EditText) findViewById(R.id.etPassword);
		etPassword.setText("111111");
		mTitleBar = (GTitleBar) findViewById(R.id.mTitleBar);
		mTitleBar.initTitle("登录");
	}
	
	 public void sina(View view){
		 setGoBack();
	    Logger.d("sina");
	    Intent i =new Intent();
		i.setClass(LoginActivity.this, SinaWeiboOAuthActivity.class);
		startActivity(i);
		Toast.makeText(LoginActivity.this, "跳转新浪微博授权页面", Toast.LENGTH_SHORT).show();
	  }
	 
	 public void qq(View view){
	    Logger.d("qq");
    	setGoBack();
	    Intent i =new Intent();
		i.setClass(LoginActivity.this, TAuthView.class);
		startActivity(i);
		Toast.makeText(LoginActivity.this, "跳转qq授权页面", Toast.LENGTH_SHORT).show();
	  }
	 
	 public void renren(View view){
	    Logger.d("renren");
	    Logger.d("qq");
    	setGoBack();
	    Intent i =new Intent();
		i.setClass(LoginActivity.this, RenRenOAuthActivity.class);
		startActivity(i);
		Toast.makeText(LoginActivity.this, "跳转人人网授权页面", Toast.LENGTH_SHORT).show();	
	  }
	 
	    @Override
		public void setGoBack() {
			Bundle bundle = new Bundle();
			bundle.putString("back2", getClass().getName());
			GoBack.goBackStack.add(bundle);
		}
	    
	    public void onLogin(View view){
	    	if(!validate()){
	    		return ;
	    	}
	    	sendRequest(Url.getParams(Url.url_login,getMap()));
	    }
	    
	    private void sendRequest(String params) {
	    	startProgress();
	    	SpecialRequest request = new SpecialRequest(Url.url_login_full);
			request.encoding = "utf-8";
			request.postParams = params;
			mHttpHandler.sendRequest(request);
		}
		
		private Map getMap(){
			Map<String, String> para = new HashMap<String, String>();
	    	para.put("email", name);
	    	para.put("password", password);
	    	para.put("token", Util.getDeviceId(this));
	    	return para;
		}
		
		private boolean validate() {
			name = etName.getText().toString().trim();
			password = etPassword.getText().toString().trim();
			if(name.equals("")){
				showToast("请填写用户名");
				return false;
			}else if(password.equals("")){
				showToast("请填写密码");
				return false;
			}
			return true;
		}
		LoginInfo s;
		@Override
		protected void httpCompleted(ResponseInterface resp) {
			SpecialResponse response = (SpecialResponse)resp;
			Logger.d("content:" + response.content);
			s = JsonParser.getLoginInfo(response.content);
			if(!s.getStatus().equals("200")){
				Message msg = handler.obtainMessage();
				msg.obj = s.getMessage();
				msg.what = 0;
				handler.sendMessage(msg);
			}else{
				MyApp.weiboUser.userid = s.getUserid();
				Logger.d("--->" + s.getUserid());
				handler.sendEmptyMessage(1);
			}
		}	
		
		Handler handler = new Handler(){
			public void handleMessage(android.os.Message msg) {
				closeProgress();
				switch(msg.what){
				case 0:
					showToast((String)msg.obj);
					break;
				case 1:
					Intent intent = new Intent(LoginActivity.this,MainActivity.class);
					startActivity(intent);
					break;
				}
			};
		};
		
		
		
		@Override
		protected void startLocate() {
		}
		@Override
		protected void stopLocate() {
		}
		
		@Override
		protected void initView() {
			// TODO Auto-generated method stub
			
		}
		@Override
		protected void getIntentData() {
			// TODO Auto-generated method stub
			
		}
		@Override
		protected void setIntentData() {
			// TODO Auto-generated method stub
			
		}

}
